package com.linln.admin.tribe.controller;

import com.linln.admin.tribe.validator.TribeValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.ad.domain.AdInfo;
import com.linln.modules.tribe.domain.Tribe;
import com.linln.modules.tribe.domain.Tribetype;
import com.linln.modules.tribe.service.TribeService;
import com.linln.modules.tribe.service.TribetypeService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 左书亮
 * @date 2019/10/10
 */
@Controller
@RequestMapping("/tribe/tribe")
public class TribeController {

    @Autowired
    private TribeService tribeService;

    @Autowired
    private TribetypeService tribetypeService;
    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("tribe:tribe:index")
    public String index(Model model, Tribe tribe) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("title", match -> match.contains())
                .withMatcher("personname", match -> match.contains());

        // 获取数据列表
        Example<Tribe> example = Example.of(tribe, matcher);
        Page<Tribe> list = tribeService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/tribe/tribe/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("tribe:tribe:add")
    public String toAdd(Model model) {
    	 model.addAttribute("tribe", new Tribe());
    	Tribetype tribetype =new Tribetype();
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<Tribetype> example = Example.of(tribetype, matcher);
        Page<Tribetype>  tribetypepage=tribetypeService.getPageList(example);
        model.addAttribute("tribelist", tribetypepage.getContent());
        return "/tribe/tribe/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("tribe:tribe:edit")
    public String toEdit(@PathVariable("id") Tribe tribe, Model model) {
        model.addAttribute("tribe", tribe);
        Tribetype tribetype =new Tribetype();
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<Tribetype> example = Example.of(tribetype, matcher);
        Page<Tribetype>  tribetypepage=tribetypeService.getPageList(example);
        model.addAttribute("tribelist", tribetypepage.getContent());
        return "/tribe/tribe/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"tribe:tribe:add", "tribe:tribe:edit"})
    @ResponseBody
    public ResultVo save(@Validated TribeValid valid, Tribe tribe) {
        // 复制保留无需修改的数据
        if (tribe.getId() != null) {
            Tribe beTribe = tribeService.getById(tribe.getId());
            EntityBeanUtil.copyProperties(beTribe, tribe);
        }else{
            tribe.setCollectnum(0);
            tribe.setCommentnum(0);
            tribe.setZannum(0);
            tribe.setHelpnum(0);
            tribe.setVisitnum(0);
        }
        // 保存数据
        Tribe newtribe= tribeService.save(tribe);
        //更新部落
        newtribe.setPersonhead(newtribe.getCreateBy().getPicture());
        newtribe.setPersonname(newtribe.getCreateBy().getNickname());
        Tribetype tt=tribetypeService.getById(newtribe.getTribeid().longValue());
        newtribe.setTribetitle(tt.getTribetitle());
        newtribe.setTribetype(tt.getTribetype());
        tribeService.save(newtribe);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("tribe:tribe:detail")
    public String toDetail(@PathVariable("id") Tribe tribe, Model model) {
        model.addAttribute("tribe",tribe);
        return "/tribe/tribe/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("tribe:tribe:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (tribeService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}